<?php
/**
 * ExaminationType Service.
 * User: Administrator
 * Date: 2020/2/13 0013
 * Time: 下午 2:02
 */
namespace app\api\service\questions;

use app\api\model\questions\ExaminationType as ExaminationTypeModels;
use app\api\model\questions\UserExaminationType;

Class ExaminationType {

    /**
     * 根据类型获取考试类型
     * @param int $type
     *
     */
    public function getType( int $type=1 )
    {
        return ExaminationTypeModels::type($type)->select();
    }

    /**
     *\获取考试类型列表
     * @return mixed
     */
    public function lists()
    {
        $lists = ExaminationTypeModels::order('id', 'asc')->selectOrFail();
        $res = [];
        $res['ksxk'][] = [
            'id' => 'xk_0',
            'value' => 0,
            'name' => '全部',
            'checked' => true
        ];
        foreach ( $lists as $_list ) {
            switch ( $_list['type'] ) {
                case 1:
                    $res['kslx'][] =
                        [
                            'id' => 'lx_'.$_list['id'],
                            'value' => $_list['id'],
                            'name' => $_list['title'],
                            'checked' => !isset($res['kslx'])
                        ];
                    break;
                case 2:
                    $res['ksxd'][] =
                        [
                            'id' => 'xd_'.$_list['id'],
                            'value' => $_list['id'],
                            'name' => $_list['title'],
                            'checked' => !isset($res['ksxd'])
                        ];
                    break;
                case 3:
                    $res['ksxk'][] =
                        [
                            'id' => 'xk_'.$_list['id'],
                            'value' => $_list['id'],
                            'name' => $_list['title'],
                            'checked' => !isset($res['ksxk'])
                        ];
                    break;
                case 4:
                    $res['ksdq'][] =
                        [
                            'id' => 'dq_'.$_list['id'],
                            'value' => $_list['id'],
                            'name' => $_list['title'],
                            'checked' => !isset($res['ksdq'])
                        ];
                    break;
            }
        }
        return $res;
    }

    /**
     * 获取用户的考试类型
     * @return mixed
     */
    public function mine(  )
    {
        return UserExaminationType::findOrFail();
    }

    /**
     * 设置用户考试类型
     * @param array $data
     * @return mixed
     */
    public function setting( array $data )
    {
        $userExaminationType = UserExaminationType::findOrEmpty();
        if ( $userExaminationType->isEmpty() === false ) {
            $userExaminationType->examination_type = $data['examination_type'];
            $userExaminationType->examination_section = $data['examination_section'];
            $userExaminationType->examination_discipline = $data['examination_discipline'];
            $userExaminationType->examination_area = $data['examination_area'];
            return $userExaminationType->save();
        }
        return UserExaminationType::create($data)->id?true:false;
    }

}